package client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import common.Terra;
import common.Acqua;
import common.Vegetazione;
import common.Carogna;

import javax.swing.*;

import common.Casella;

public class VisualeDino extends JFrame implements ActionListener,Serializable{
	
	private JButton[][] caselle;
	
	private int dx;
	private int dy;
	private int x;
	private int y;
	private int [] risp = new int [2];
	
	private ImageIcon terra = new ImageIcon( "terriccio.jpg");
	private ImageIcon vegetazione=new ImageIcon("vegetazione.jpg");
	private ImageIcon acqua=new ImageIcon("acqua.jpg");
	private ImageIcon grigio=new ImageIcon("grigio.gif");
	private ImageIcon carogna=new ImageIcon("burger.jpg");
	private ImageIcon dino=new ImageIcon("dinosauro.jpg");
	
	public VisualeDino(int x,int y,int dimx,int dimy, Casella[][] vista){
				
		super("Visuale dinosauro");
		setLayout(new FlowLayout());
		
		dx = dimx;
		dy = dimy;
		this.x=x;
		this.y=y;
				
		caselle = new JButton[dimx][dimy];
		JPanel pannello = new JPanel();
		pannello.setLayout(new GridLayout(dimx,dimy));
		
		for (int i=0;i<dimx;i++)
			for (int j=0;j<dimy;j++){
				caselle[i][j]= new JButton();
									
				if(vista[i][j]==null)
					caselle[i][j].setIcon(grigio);
				else if(!(vista[i][j].getLibera())){
					caselle[i][j].setToolTipText("Dinosauro "+vista[i][j].getDino().getId());
					caselle[i][j].setIcon(dino);
				}
				else if(vista[i][j] instanceof Terra){
					if((vista[i][j].getPosizioneI()<0)||(vista[i][j].getPosizioneJ()<0)||(vista[i][j].getPosizioneI()>39)||(vista[i][j].getPosizioneJ()>39))
						caselle[i][j].setIcon(grigio);
					else
						caselle[i][j].setIcon(terra);
				}
				else if(vista[i][j] instanceof Acqua)
					caselle[i][j].setIcon(acqua);
				else if(vista[i][j] instanceof Vegetazione){
					caselle[i][j].setToolTipText(""+vista[i][j].getEnergia());
					caselle[i][j].setIcon(vegetazione);
				}
				else if(vista[i][j] instanceof Carogna){
					caselle[i][j].setIcon(carogna);
					caselle[i][j].setToolTipText(""+vista[i][j].getEnergia());
				}	
				
				caselle[i][j].setPreferredSize(new Dimension(75, 75));
				caselle[i][j].addActionListener(this);
				pannello.add(caselle[i][j]);
				if((i==dimx-1)&&(j==dimy-1))
					break;
			}
		
		add(pannello);
		
		pack();
		validate();
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		synchronized(this){
			for(int i=0;i<dx;i++)
				for(int j=0;j<dy;j++){
				if(e.getSource().equals(caselle[i][j])){
				//restituisco coordinate casella destinazione
					risp[0]=x-(dx-1)+i;
					risp[1]=y+j;
					
				notifyAll();
				}
			}
		}
	}
	
	public int[] getRisp(){
		synchronized(this){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		dispose();
		return risp;
	}
}
	
	
